/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;

/**
 * 说明：
 * 商品图片
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TCommodityPicture;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.FileUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.jpanel.PicturePanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.factory.UIFactory;
import com.ruipengkj.service.TCommodityPictureService;
import com.ruipengkj.view.base.BasePanel;

public class DataCommodityPicturePanel extends BasePanel{

	private static final long serialVersionUID = 1L;

	private JButton selectButton;//选择本地图片
	private JButton delButton;//删除当前图片
	private JButton prevButton = UIFactory.getInstance().createJButton(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV), this, false, false);
	private JButton nextButton = UIFactory.getInstance().createJButton(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT), this, false, false);
	
	private TCommodityPictureService TCommodityPictureService = BuildSpringApplication.getInstance().getTCommodityPictureServiceImpl();
	
	private List<String> delCommodityPicture = new ArrayList<String>();
	private List<CommodityBufferedImage> images = new ArrayList<CommodityBufferedImage>();
	private int imageID = 0;
	private int maxImageID = 0;
	
	public DataCommodityPicturePanel() {
		init();
		
		prevButton.setRolloverIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV_CLICK));
		prevButton.setRolloverSelectedIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV_CLICK));
		nextButton.setRolloverIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT_CLICK));
		nextButton.setRolloverSelectedIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT_CLICK));
	}
	
	private void init() {
		Color color = new Color(244, 245, 246);
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.setBackground(new Color(244, 245, 246));
		
		mainPanel.add(topPanel(), BorderLayout.NORTH);
		mainPanel.add(centerPanel());
		
		this.setLayout(new BorderLayout());
		this.add(ColorPanel(30, 30), BorderLayout.SOUTH);
		this.add(new JLabel("                "), BorderLayout.WEST);
		this.add(new JLabel("                "), BorderLayout.EAST);
		this.add(mainPanel);
		this.setOpaque(true);
		this.setBackground(color);
	}
	
	private JPanel ColorPanel(int x, int y) {
		JPanel jp = new JPanel();
		Util.setSize(jp, x, y);
		jp.setOpaque(false);
		return jp;
	}
	
	private JPanel topPanel() {
		JPanel top_panel = uiFactory.createGridJPanel(1, 3);
		
		selectButton = new JButton("选择本地图片");
		selectButton.addActionListener(this);
		delButton = new JButton("删除图片");
		delButton.addActionListener(this);
		JPanel jp = new JPanelFlowLayoutComponent(selectButton, FlowLayout.CENTER);
		JPanel jp2 = new JPanelFlowLayoutComponent(delButton, FlowLayout.RIGHT);
		
		top_panel.add(new JLabel("商品图片集：", JLabel.LEFT));
		top_panel.add(jp);
		top_panel.add(jp2);
		
		return top_panel;
	}
	
	private PicturePanel pictureMax = new PicturePanel();
	private PicturePanel pictureMin1 = new PicturePanel();
	private PicturePanel pictureMin2 = new PicturePanel();
	private PicturePanel pictureMin3 = new PicturePanel();
	private PicturePanel pictureMin4 = new PicturePanel();
	private JPanel centerPanel() {
		JPanel center_panel = new JPanel(new BorderLayout());
		center_panel.setBackground(Color.white);
		center_panel.setBorder(BorderFactory.createTitledBorder(""));
		
		pictureMin1.addMouseListener(this);
		pictureMin2.addMouseListener(this);
		pictureMin3.addMouseListener(this);
		pictureMin4.addMouseListener(this);
		
		pictureMin1.setOpaque(false);
		pictureMin2.setOpaque(false);
		pictureMin3.setOpaque(false);
		pictureMin4.setOpaque(false);
		pictureMin1.setBorder(BorderFactory.createTitledBorder(""));
		pictureMin2.setBorder(BorderFactory.createTitledBorder(""));
		pictureMin3.setBorder(BorderFactory.createTitledBorder(""));
		pictureMin4.setBorder(BorderFactory.createTitledBorder(""));
//		Util.setSize(pictureMax, 50, 50);
		Util.setSize(pictureMin1, 82, 60);
		Util.setSize(pictureMin2, 82, 60);
		Util.setSize(pictureMin3, 82, 60);
		Util.setSize(pictureMin4, 82, 60);
		JPanel jp = uiFactory.createFlowJPanel();
		jp.add(prevButton);
		jp.add(pictureMin1);
		jp.add(pictureMin2);
		jp.add(pictureMin3);
		jp.add(pictureMin4);
		jp.add(nextButton);
		
		JPanel jp3 = uiFactory.createBorderJPanel();
		jp3.add(ColorPanel(1, 1), BorderLayout.WEST);
		jp3.add(ColorPanel(1, 1), BorderLayout.EAST);
		jp3.add(pictureMax);
		
		JPanel jp2 = uiFactory.createBorderJPanel();
		jp2.add(jp3);
		jp2.add(jp, BorderLayout.SOUTH);
		
		center_panel.add(ColorPanel(1, 1), BorderLayout.SOUTH);
		center_panel.add(ColorPanel(140, 140), BorderLayout.WEST);
		center_panel.add(ColorPanel(140, 140), BorderLayout.EAST);
		center_panel.add(ColorPanel(1, 1), BorderLayout.NORTH);
		
		center_panel.add(jp2);
		return center_panel;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == selectButton) {
			if (selectFile("选择商品图片")) {
				imageID = 0;
				lookPrice(0);
			}
		} else if (e.getSource() == prevButton){
			if (imageID >= 4) {
				imageID -= 4;
				lookPrice(imageID);
			}
		} else if (e.getSource() == nextButton) {
			if (images != null && imageID < images.size()-4) {
				imageID += 4;
				lookPrice(imageID);
			}
		} else if (e.getSource() == delButton) {
			if (images == null || images.size() == 0) {
				Util.showMessageDialog("没有图片！");
				return;
			}
			if (images.get(maxImageID).gettCommodityPictureID() != null) {
				delCommodityPicture.add(images.get(maxImageID).gettCommodityPictureID());
			}
			images.remove(maxImageID);
			if (maxImageID != 0) {
				--maxImageID;
			}
			if (imageID >= images.size()) {
				if (imageID >= 4)
					imageID -= 4;
				else 
					imageID = 0;
			}
			lookPrice(imageID);
		}
	}
	
	private void lookPrice(int i) {
		if (images == null)
			return;
		int length = images.size();
		if (length > i) {
			maxImageID = i;
			pictureMax.setImage(images.get(i).getImage());
			pictureMin1.setImage(images.get(i).getImage());
		} else {
			pictureMax.setImage((Image)null);
			pictureMin1.setImage((Image)null);
		}
		if (length > i+1)
			pictureMin2.setImage(images.get(i+1).getImage());
		else 
			pictureMin2.setImage((Image)null);
		if (length > i+2)
			pictureMin3.setImage(images.get(i+2).getImage());
		else 
			pictureMin3.setImage((Image)null);
		if (length > i+3)
			pictureMin4.setImage(images.get(i+3).getImage());
		else 
			pictureMin4.setImage((Image)null);
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if (images == null || images.size() == 0)
			return;
		int i = 0;
		
		if (e.getSource() == pictureMin1) {
			i = 0;
		} else if (e.getSource() == pictureMin2) {
			i = 1;
		} else if (e.getSource() == pictureMin3) {
			i = 2;
		} else if (e.getSource() == pictureMin4) {
			i = 3;
		}
		
		if (images.size() > imageID+i) {
			maxImageID = imageID + i;
			pictureMax.setImage(images.get(imageID+i).getImage());
		}
	}
	
	// 选择本地文件
	private boolean selectFile(String title) {
		JFileChooser fileChooser = null;
		fileChooser = new JFileChooser();
		fileChooser.setMultiSelectionEnabled(true);
		fileChooser.setFileFilter(FileUtil.getFileFilter());
		fileChooser.showOpenDialog(null);
		File[] files = fileChooser.getSelectedFiles();
		if (files != null && files.length > 0) {
			for (int i = 0, length = files.length; i < length; ++i) {
				CommodityBufferedImage CommodityBufferedImage = new CommodityBufferedImage();
				BufferedImage image = null; 
				try {
					image = ImageIO.read(files[i]);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if(image == null) {
					JOptionPane.showMessageDialog(null, "请确认你所选择的是图片");
					return false;
				} else {
					CommodityBufferedImage.setImage(image);
					images.add(CommodityBufferedImage);
				}
			}
			return true;
		}
		return false;
	}
	
	public void save(TCommodity tCommodity) throws Exception {
		
		if (images != null && images.size() != 0) {
			for(int i = 0, length = images.size(); i < length; ++i) {
				if (images.get(i).gettCommodityPictureID() == null) {
					TCommodityPicture tcommodityPicture = new TCommodityPicture();
					tcommodityPicture.setId(UUID.GenUUID15());
					tcommodityPicture.setTCommodity(tCommodity);
					tcommodityPicture.setPicture(Util.ImageToBlob(images.get(i).getImage()));
					TCommodityPictureService.saveOrUpdateService(tcommodityPicture);
				}
			}
		}
		for (int i = delCommodityPicture.size()-1; i >= 0; --i) {
			TCommodityPictureService.deleteService(delCommodityPicture.get(i));
		}
	}
	
	public void dataShow(final TCommodity tCommodity) {
		if (tCommodity != null) {
			new Thread(){
				public void run(){
				List<TCommodityPicture> tCommodityPictures;
				try {
					tCommodityPictures = TCommodityPictureService.findService(new TCommodityPicture(null, tCommodity));
					if (tCommodityPictures != null && tCommodityPictures.size() != 0) {
						int length = tCommodityPictures.size();
						for (int i = 0; i < length; ++i) {
							CommodityBufferedImage CommodityBufferedImage = new CommodityBufferedImage();
							TCommodityPicture tCommodityPicture = tCommodityPictures.get(i);
							CommodityBufferedImage.setImage(Util.BlobToImage(tCommodityPicture.getPicture()));
							CommodityBufferedImage.settCommodityPictureID(tCommodityPicture.getId());
							images.add(CommodityBufferedImage);
						}
						lookPrice(0);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			}.start();
		}
	}
	
	@Override
	public void clear() {
		
	}
	
	class CommodityBufferedImage{
		private String tCommodityPictureID = null;
		private BufferedImage image;
		
		public String gettCommodityPictureID() {
			return tCommodityPictureID;
		}
		public void settCommodityPictureID(String tCommodityPictureID) {
			this.tCommodityPictureID = tCommodityPictureID;
		}
		public BufferedImage getImage() {
			return image;
		}
		public void setImage(BufferedImage image) {
			this.image = image;
		}
	}
}
